package com.kakavr.order.service;

import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Objects;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.base.service.BaseService;
import com.kakavr.order.dao.IPurchaseHistoryDao;
import com.kakavr.page.Pagination;
import com.kakavr.page.constant.PaginationConstant;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.shop.service.IShopAdminService;
import com.kakavr.user.service.IUserService;
import com.kakavr.util.DateTimeUtils;

@Service
public class PurchaseHistoryService extends BaseService implements
		IPurchaseHistoryService {
	@Resource
	private IPurchaseHistoryDao purchaseHistoryDao;
	@Resource
	private IUserService userService;
	@Resource
	private IShopAdminService shopAdminService;

	@Override
	public void addPurchaseHistory(Map<String, Object> map) {
		if (null != map.get("userId")) {//如果是玩家登录
			System.out.println("++++++++++++++++++++++++++++++++++++++++++++userid"+map.get("userId")+JSON.toJSONString(map));
			this.userPlayGames(map);
		}else if(null != map.get("empId")){//如果是管理员登录
			this.empPlayGames(map);
		}
		map.put("createTime", DateTime.now().toDate());
		purchaseHistoryDao.insertPurchaseHistory(map);
		map.put("purchaseID", map.get("id"));
		this.addPurGame(map);
		
	}
	
	private void empPlayGames(Map<String,Object> map){
		Map<String, Object> store = purchaseHistoryDao.selectShopTimes(map);
		if (null == store || null == store.get("times")) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_4003,
					MsgConstants.MSG_CONTENT_4003);
		}
		long times = (long) store.get("times"), one = NumberUtils.LONG_ONE;
		if (times < one) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_4001,
					MsgConstants.MSG_CONTENT_4001);
		}
		store.put("times", times-one);
		shopAdminService.updateShop(store);		
	}
	/**
	 * 扣费业务处理
	 * @param map
	 */
	private void userPlayGames(Map<String,Object> map){
		Map<String, Object> user = userService.getUserInfo(map);
		if (null == user || null == user.get("times")) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_4002,
					MsgConstants.MSG_CONTENT_4002);
		}
		long times = (long) user.get("times"), zero = NumberUtils.LONG_ZERO, one = NumberUtils.LONG_ONE;
		if (times <= zero) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_4001,
					MsgConstants.MSG_CONTENT_4001);
		}
		user.clear();
		user.put("id", map.get("userId"));
		user.put("times", times - one);
		userService.updateUser(user);
		//活动用户不参与店铺 分成
		if(Objects.equal(1, user.get("isActivity"))){
			return;
		}
		Map<String,Object> param = Maps.newHashMap();
		param.put("userId", map.get("userId"));
		param.put("storeId", map.get("storeID"));
		//是否在本店铺有可用 的次数
		Map<String,Object> recharge = purchaseHistoryDao.selectRechargeInfo(param);//查询出来消费的店铺
		System.out.println("+++++++++++++++++++++++++查出来消费店铺次数++++++++++++++++++++++++++"+JSON.toJSONString(recharge));
		if(null != recharge && !recharge.isEmpty()){
			purchaseHistoryDao.updateRechargeInfo(recharge);
			map.put("rechargeId", recharge.get("id"));
			return;
		}
		param.remove("storeId");//删除掉查询玩的店铺的充值信息
		recharge = purchaseHistoryDao.selectRechargeInfo(param);//查出来原店铺次数
		System.out.println("+++++++++++++++++++++++++查出来原店铺次数++++++++++++++++++++++++++"+JSON.toJSONString(recharge));
		if(null != recharge && !recharge.isEmpty()){
			System.out.println("+++++++++++++++++++++++++进入保存方法++++++++++++++++++++++++++");
			List<Map<String, Object>> list = Lists.newArrayList();
			Map<String, Object> income = Maps.newHashMap();
			income.put("storeId", map.get("storeID"));
			income.put("amount", 8);
			income.put("category", 0);
			income.put("orderId", map.get("purchaseID"));
			income.put("rechargeId", recharge.get("id"));
			income.put("sourceId", recharge.get("storeId"));
			income.put("createTime", DateTimeUtils.dateToStringSFM(new Date()));
			list.add(income);
			Map<String, Object> expMap = Maps.newHashMap();
			expMap.put("storeId", recharge.get("storeId"));
			expMap.put("amount", "-8");
			expMap.put("category", 1);
			expMap.put("orderId", map.get("purchaseID"));
			expMap.put("rechargeId", recharge.get("id"));
			expMap.put("sourceId", map.get("storeID"));
			expMap.put("createTime",DateTimeUtils.dateToStringSFM(new Date()));
			list.add(expMap);
			map.put("storeList", list);
			System.out.println("+++++++++++++++++++++++++打印出来map的值++++++++++++++++++++++++++"+JSON.toJSONString(map));
			purchaseHistoryDao.insertTradeFlowBatch(map);
			map.put("rechargeId", recharge.get("id"));
			purchaseHistoryDao.updateRechargeInfo(recharge);
			return;
		}
		/*throw new ValidateParamException(MsgConstants.MSG_CODE_4001,
				MsgConstants.MSG_CONTENT_4001);*/
	}

	@Override
	public void addPurGame(Map<String, Object> map) {
		if (null == map.get("createTime")) {
			map.put("createTime", DateTime.now().toDate());
		}
		purchaseHistoryDao.insertGameHistory(map);
		map.put("detailID", map.get("id"));
	}

	@Override
	public void updatePurchaseHistory(Map<String, Object> map) {
		map.put("finishTime", DateTime.now().toDate());
		if ("purOver".equals(map.get("type"))) {// 订单结束
			purchaseHistoryDao.updatePurchaseHistory(map);
		}
		purchaseHistoryDao.updateGameHistory(map);
	}

	@Override
	public Pagination<Map<String, Object>> selectPurchaseListByPage(
			Map<String, Object> map) {
		PageHelper.startPage(NumberUtils.toInt((String) map
				.get(PaginationConstant.PAGE_NO_NAME)), NumberUtils
				.toInt((String) map.get(PaginationConstant.PAGE_SIZE_NAME)));
		return new Pagination<Map<String, Object>>(
				purchaseHistoryDao.selectPurchaseList(map));
	}

	@Override
	public Map<String, Object> selectPurchaseInfo(Map<String, Object> map) {
		List<Map<String, Object>> list = purchaseHistoryDao
				.selectPurchaseList(map);
		if (null != list && !list.isEmpty()) {
			Map<String, Object> purchMap = list.get(0);
			purchMap.put("gameList", purchaseHistoryDao.selectGameHistory(map));
			return purchMap;
		}
		return null;
	}

	@Override
	public Pagination<Map<String, Object>> selectGameHistoryList(
			Map<String, Object> map) {
		PageHelper.startPage(NumberUtils.toInt((String) map
				.get(PaginationConstant.PAGE_NO_NAME)), NumberUtils
				.toInt((String) map.get(PaginationConstant.PAGE_SIZE_NAME)));
		return new Pagination<Map<String,Object>>(purchaseHistoryDao.selectGameHistoryList(map));
	}

	@Override
	public List<Map<String, Object>> selectEvaluateList() {
		return purchaseHistoryDao.selectEvaList();
	}
	
	@Override
	public Map<String, Object> selectNowPurchaseHistoryId(Map<String, Object> map) {
		return purchaseHistoryDao.selectNowPurchaseHistoryId(map);
	}

	@Override
	public void updatePurchaseHistoryByEva(Map<String, Object> map) {
		purchaseHistoryDao.updatePurchaseHistory(map);
	}

}
